package malus.shaofeng.com.beiwomusic.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.model.Content;
import malus.shaofeng.com.beiwomusic.model.ImageContent;
import malus.shaofeng.com.beiwomusic.model.TextContent;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;

/**
 * Created by aaa on 15-3-12.
 */
public class NewStoryContentAdapter extends BaseAdapter {

    private Context context;
    private List<Content> contentList;
    private BitmapUtils bitmapUtils;
    private String path;

    public NewStoryContentAdapter(Context context, List<Content> contents) {
        this.context = context;
        this.contentList = contents;
    }

    @Override
    public int getCount() {
        return contentList.size();
    }

    @Override
    public Object getItem(int position) {
        return contentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (convertView!=null){
            ret=convertView;
        }else {
            ret= LayoutInflater.from(context).inflate(R.layout.item_newstory_content,parent,false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder==null){
            holder = new ViewHolder();
            holder.image=(ImageView)ret.findViewById(R.id.imageContent);
            holder.text=(TextView)ret.findViewById(R.id.textContent);
            ret.setTag(holder);
        }
        Log.d("contentList", contentList.size() + "");
        Content content = contentList.get(position);

        holder.text.setVisibility(View.GONE);
        holder.image.setVisibility(View.GONE);

        if (content instanceof TextContent){
            String value = ((TextContent) content).getValue();
            Log.d("contentList", "text: " + value);
            holder.text.setVisibility(View.VISIBLE);
            holder.text.setText(value);

        }else if(content instanceof ImageContent){
            holder.image.setVisibility(View.VISIBLE);
            bitmapUtils = BitmapHelper.getBitmapUtils();
            String value = ((ImageContent) content).getValue();
            if (value!=null){
                path = BitmapHelper.getImageUrl(value);
                 bitmapUtils.display(holder.image,path);
            }

        }
        return ret;
    }

    private static class ViewHolder{
        public ImageView image;
        public TextView text;
    }
}
